package com.oocl.ha.merchant.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import com.oocl.ha.entity.Merchant;
import com.oocl.ha.merchant.util.EncryptUtil;

@Aspect
@Component
public class SecurityAspect {
	private final String merchantRegister = "execution(* com.oocl.ha.merchant.service.MerchantManager.merchantRegister(..))";
	private final String merchantLogin = "execution(* com.oocl.ha.merchant.service.MerchantManager.merchantLogin(..))";

	@Around(merchantLogin)
	public Object loginMdProcess(ProceedingJoinPoint jp) {
		Object o = null;
		String password = (String) jp.getArgs()[1];
		try {
			o = jp.proceed(new Object[] { jp.getArgs()[0], EncryptUtil.md(password, "md5") });
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return o;
	}

	@Around(merchantRegister)
	public Object registMdProcess(ProceedingJoinPoint jp) {
		Object o = null;
		Merchant merchant = (Merchant) jp.getArgs()[0];
		merchant.setPassword(EncryptUtil.md(merchant.getPassword(), "md5"));
		try {
			o = jp.proceed();
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return o;
	}
}
